import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:app_core/app_core.dart';
import '../../../common/managers/user_manager.dart';
import '../../../common/managers/theme_manager/theme_manager.dart';
import '../../../common/constant/colors.dart';
import '../../../common/constant/icon.dart';
import '../../../common/constant/styles.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Get.theme.colorScheme.background,
      // appBar: APPBar.textBar(
      //   '智能家居',
      //   showBack: false,
      //   color: Get.theme.colorScheme.surface,
      //   titleColor: Get.theme.colorScheme.onSurface,
      //   right: Row(
      //     mainAxisSize: MainAxisSize.min,
      //     children: [
      //       AppGesture(
      //         onTap: () {
      //           Get.log('添加设备');
      //         },
      //         child: Padding(
      //           padding: EdgeInsets.all(8),
      //           child: SvgPicture.asset(AppIcon.icHomeAddDevice, width: 24, height: 24),
      //         ),
      //       ),
      //       SizedBox(width: 8),
      //       AppGesture(
      //         onTap: () {
      //           Get.log('消息');
      //         },
      //         child: Padding(
      //           padding: EdgeInsets.all(8),
      //           child: SvgPicture.asset(AppIcon.icHomeMsg, width: 24, height: 24),
      //         ),
      //       ),
      //     ],
      //   ),
      // ),
      body: GetBuilder<UserManager>(
        builder: (userManager) {
          return SingleChildScrollView(
            padding: EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: AppSpaces.edgeMd,
                  decoration: BoxDecoration(
                    color: AppColor.white,
                    borderRadius: AppSpaces.borderRadiusMd,
                  ),
                  child: Row(
                    children: [
                      CircleAvatar(
                        radius: 30,
                        backgroundColor: Get.theme.colorScheme.primary.withOpacity(0.1),
                        child: Icon(
                          Icons.person,
                          size: 30,
                          color: Get.theme.colorScheme.primary,
                        ),
                      ),
                      SizedBox(width: 16),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              userManager.userModel?.nickName ?? '未登录',
                              style: AppStyles.s18w5,
                            ),
                            SizedBox(height: 4),
                            Text(
                              userManager.userModel?.email ?? '',
                              style: AppStyles.s14.copyWith(color: Get.theme.colorScheme.onSurface.withOpacity(0.6)),
                            ),
                          ],
                        ),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 16,
                        color: Get.theme.colorScheme.onSurface.withOpacity(0.6),
                      ),
                    ],
                  ),
                ),
                
                SizedBox(height: 16),
                
                // 功能菜单
                _buildSectionTitle('设备管理'),
                SizedBox(height: 8),
                _buildMenuItem(
                  icon: Icons.devices,
                  title: '我的设备',
                  subtitle: '管理您的智能设备',
                  onTap: () {
                    Get.log('我的设备');
                  },
                ),
                _buildMenuItem(
                  icon: Icons.schedule,
                  title: '定时任务',
                  subtitle: '设置设备定时开关',
                  onTap: () {
                    Get.log('定时任务');
                  },
                ),
                
                SizedBox(height: 16),
                
                _buildSectionTitle('数据分析'),
                SizedBox(height: 8),
                _buildMenuItem(
                  icon: Icons.analytics,
                  title: '使用统计',
                  subtitle: '查看设备使用数据',
                  onTap: () {
                    Get.log('使用统计');
                  },
                ),
                _buildMenuItem(
                  icon: Icons.trending_up,
                  title: '能耗分析',
                  subtitle: '分析设备能耗情况',
                  onTap: () {
                    Get.log('能耗分析');
                  },
                ),
                
                SizedBox(height: 16),
                
                _buildSectionTitle('系统设置'),
                SizedBox(height: 8),
                _buildMenuItem(
                  icon: Icons.settings,
                  title: '应用设置',
                  subtitle: '应用配置和偏好设置',
                  onTap: () {
                    Get.log('应用设置');
                  },
                ),
                _buildMenuItem(
                  icon: Icons.help_outline,
                  title: '帮助中心',
                  subtitle: '使用帮助和常见问题',
                  onTap: () {
                    Get.log('帮助中心');
                  },
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _buildSectionTitle(String title) {
    return Text(
      title,
      style: AppStyles.s16w5.copyWith(color: Get.theme.colorScheme.primary),
    );
  }

  Widget _buildMenuItem({
    required IconData icon,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return Container(
      margin: EdgeInsets.only(bottom: 8),
      decoration: BoxDecoration(
        color: AppColor.white,
        borderRadius: AppSpaces.borderRadiusMd,
      ),
      child: AppGesture(
        onTap: onTap,
        child: Padding(
          padding: AppSpaces.edgeMd,
          child: Row(
            children: [
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                  color: Get.theme.colorScheme.primary.withOpacity(0.1),
                  borderRadius: AppSpaces.borderRadiusSm,
                ),
                child: Icon(
                  icon,
                  color: Get.theme.colorScheme.primary,
                  size: 20,
                ),
              ),
              SizedBox(width: 16),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: AppStyles.s16w5,
                    ),
                    SizedBox(height: 4),
                    Text(
                      subtitle,
                      style: AppStyles.s14.copyWith(color: Get.theme.colorScheme.onSurface.withOpacity(0.6)),
                    ),
                  ],
                ),
              ),
              Icon(
                Icons.arrow_forward_ios,
                size: 16,
                color: AppColor.textSecondary,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
